//
//  BaseViewController.m
//  TabDemo
//
//  Created by weiwang on 2015-11-18.
//  Copyright 2015年 weiwang. All rights reserved.
//

#import "OPBaseViewController.h"
#import "UIColor+Hex.h"
#import "ZYBViewDefine.h"

@implementation OPBaseViewController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kOPBackgroundColor;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:self.navgationBar];
    [self layoutSuperSubview];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)layoutSuperSubview {
    // 布局子控件
    // 自定义导航栏
    [self.navgationBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(kNavigationBarBottom);
    }];
}


- (OPHomeNavgationBar *)navgationBar {
    if (!_navgationBar) {
        _navgationBar = [[OPHomeNavgationBar alloc] initWithFrame:CGRectZero];
    }
    return _navgationBar;
}

- (void)setDefaultNavigationBarBackgroundImage
{
    //[_viewControllerHelper setNavgationBarImage:[self navBarBlueBackgroundImg]];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
       // _viewControllerHelper = [[ViewControllerHelper alloc] init];
        //_viewControllerHelper.ownerViewController = self;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       // _viewControllerHelper = [[ViewControllerHelper alloc] init];
        //_viewControllerHelper.ownerViewController = self;
    }
    return self;
}

- (void)setDefaultBackItemWithBackArrow{
    if ((self.navigationController && [self.navigationController.viewControllers firstObject] != self) || !self.navigationController){
        //[_viewControllerHelper setDefaultBackItemWithImage:[UIImage imageNamed:@"nav_bar_back.png"] selectedImage:[UIImage imageNamed:@"nav_bar_back_pressed.png"]];
    }else{
        //[_viewControllerHelper setDefaultBackItemWithImage:[UIImage imageNamed:@"Action_close_white"] selectedImage:[UIImage imageNamed:@"Action_close_white"]];
    }
}

- (void)setDefaultBackItemWithDimissArrow{
   // [_viewControllerHelper setDefaultBackItemWithImage:[UIImage imageNamed:@"Action_close_white"] selectedImage:[UIImage imageNamed:@"Action_close_white"]];
}

- (void)setDefaultBackItemWithHide{
    [self setLeftItemImage:nil selectedImage:nil target:nil action:nil];
}

//设置navbar左边按钮（图片）
- (void)setLeftItemImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action
{
   // [_viewControllerHelper setLeftItemImage:image selectedImage:selectedImage target:target action:action];
}

//设置navbar右边按钮（图片）
- (void)setRightItemImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action
{
   // [_viewControllerHelper setRightItemImage:image selectedImage:selectedImage target:target action:action];
}

- (void)setLeftItemText:(NSString *)text target:(id)target action:(SEL)action {
   // [_viewControllerHelper setLeftItemText:text target:target action:action];
}

//设置navbar右边按钮（文字）
- (void)setRightItemText:(NSString *)text target:(id)target action:(SEL)action{
    //[_viewControllerHelper setRightItemText:text target:target action:action];
}

- (UIBarButtonItem *)barWithText:(NSString *)text
                          target:(id)target
                          action:(SEL)action{
    return  nil;//[_viewControllerHelper barWithText:text target:target action:action];
    
}

- (UIBarButtonItem *)barWithImage:(UIImage *)image
                    selectedImage:(UIImage *)selectedImage
                  backgroundImage:(UIImage *)backgroundImage
          selectedBackgroundImage:(UIImage *)selectedBackgroundImage
                           target:(id)target
                           action:(SEL)action {

  //  return nil; // [_viewControllerHelper barWithImage:image selectedImage:selectedImage backgroundImage:backgroundImage selectedBackgroundImage:selectedBackgroundImage target:target action:action];
    return nil;
}

- (void)setNavgationBarImage:(UIImage *)image
{
   // [_viewControllerHelper setNavgationBarImage:image];
}



//- (void)showMessageOnlyTip:(NSString*)msg withTipType:(VCShowTipType)type
//{
//    [_viewControllerHelper showMessageOnlyTip:msg withTipType:type];
//}




- (void)showErrorWithError:(NSError *)error {
//    if (error.code == -3) {
//        [_viewControllerHelper showMessageOnlyTip:kNetworkNotReachable withTipType:VCShowTipTypeNetError];
//    } else {
//        
//        NSString *errorString = error.localizedDescription;
//        if (!errorString) {
//            errorString = [[ZYBErrorMsgHelper sharedInstance] getErrMesage:error];
//        }
//        if (!errorString) {
//            errorString = kNetRequireFailed;
//        }
//        [_viewControllerHelper showMessageOnlyTip:errorString withTipType:VCShowTipTypeNetError];
//    }
}

- (void)showAlertWithMessage:(NSString *)msg
                      OtherChoice:(NSString *)otherChoice
                  completion:(dispatch_block_t)completion{
   // [_viewControllerHelper showAlertWithMessage:msg OtherChoice:otherChoice completion:completion];
}

//- (BOOL)validResponse:(id)response
//{
//    return [_viewControllerHelper validResponse:response];
//}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    DDLogInfo(@"%@,%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
//    [(appde)[[UIApplication sharedApplication] delegate] cur]
    
//    if ([[self.navigationController viewControllers] count] > 1){
//        
//    }else{
//        [self.navigationItem setLeftBarButtonItem:nil];
//    }
//    DDLogInfo(@"%@,%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    DDLogInfo(@"%@,%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

- (UIImage *)navBarBlueBackgroundImg{
    static UIImage *img = nil;
    if (img == nil) {
        float height = [[UIDevice currentDevice].systemVersion floatValue] >= 7 ? 64 : 44;
        CGRect rect = CGRectMake(0, 0, 10, height);
        UIGraphicsBeginImageContext(rect.size);
        [[UIColor blueColor] setFill];//0x2d8dfa
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextFillRect(context, rect);
        
        img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext() ;
        
    }
    return img;
}

- (void)setCanSwipeBack:(BOOL)canSwipe {
    for(UIGestureRecognizer *ges in self.navigationController.view.gestureRecognizers) {
        if ([ges isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
            ges.enabled = canSwipe;
        }
    }
}

- (void)setDefaultBackItemWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage {
   // [_viewControllerHelper setDefaultBackItemWithImage:image selectedImage:selectedImage];
}

- (void)disableScrollViewScrollWhileEdgePag:(UIScrollView *)scrollView{
    NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
    //当是侧滑手势的时候设置scrollview需要此手势失效才生效即可
    for (UIGestureRecognizer *gesture in gestureArray) {
        if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
            [scrollView.panGestureRecognizer requireGestureRecognizerToFail:gesture];
        }
    }
}
@end
